package org.dromara.merchants.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.merchants.domain.bo.TaAssociationBo;
import org.dromara.merchants.domain.vo.TaAssociationVo;
import org.dromara.merchants.domain.TaAssociation;
import org.dromara.merchants.mapper.TaAssociationMapper;
import org.dromara.merchants.service.ITaAssociationService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 关联Service业务层处理
 *
 * @author xiyin321
 * @date 2024-07-08
 */
@RequiredArgsConstructor
@Service
public class TaAssociationServiceImpl implements ITaAssociationService {

    private final TaAssociationMapper baseMapper;

    /**
     * 查询关联
     */
    @Override
    public TaAssociationVo queryById(Long associationId){
        return baseMapper.selectVoById(associationId);
    }

    /**
     * 查询关联列表
     */
    @Override
    public TableDataInfo<TaAssociationVo> queryPageList(TaAssociationBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TaAssociation> lqw = buildQueryWrapper(bo);
        Page<TaAssociationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询关联列表
     */
    @Override
    public List<TaAssociationVo> queryList(TaAssociationBo bo) {
        LambdaQueryWrapper<TaAssociation> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TaAssociation> buildQueryWrapper(TaAssociationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TaAssociation> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getParentId() != null, TaAssociation::getParentId, bo.getParentId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), TaAssociation::getName, bo.getName());
        lqw.eq(bo.getQuantity() != null, TaAssociation::getQuantity, bo.getQuantity());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), TaAssociation::getType, bo.getType());
        return lqw;
    }

    /**
     * 新增关联
     */
    @Override
    public Boolean insertByBo(TaAssociationBo bo) {
        TaAssociation add = MapstructUtils.convert(bo, TaAssociation.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setAssociationId(add.getAssociationId());
        }
        return flag;
    }

    /**
     * 修改关联
     */
    @Override
    public Boolean updateByBo(TaAssociationBo bo) {
        TaAssociation update = MapstructUtils.convert(bo, TaAssociation.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TaAssociation entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除关联
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
